﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace ZR.Common
{
   
    public class Tools
    {
        /// <summary>
        /// 身份证验证
        /// </summary>
        /// <param name="identityCard"></param>
        /// <returns>正确 true</returns>
        public static bool IsValidChineseId(string identityCard)
        {
            string pattern = @"^[1-9]\d{5}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$";
            return Regex.IsMatch(identityCard, pattern);
        }
        public static int getdays(DateTime date1, DateTime date2)
        {

            TimeSpan timeSpan = date2.Subtract(date1);
            int days = timeSpan.Days;
            return days;
        }


        public static string getphone(string phone)
        {
            return Regex.Replace(phone, "(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }

        public static double objToDouble(double obj)
        {
            try
            {

                return Math.Round(obj, 2, MidpointRounding.AwayFromZero); //Returns 45.37
            }
            catch
            {
                return 0;
            }
        }
        public static string substring(string originalString, int num)
        {
            if (originalString.Length <= num)
            {
                return originalString;
            }
            string substring2 = originalString.Substring(0, num);
            return substring2;
        }

    

        private static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z",  "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

        /// <summary>
        /// 生成优惠券的唯一编码(32位的Guid，每4位处理变成1位，结果变成8位的优惠券编码)
        /// </summary>
        /// <returns></returns>
        public static string createcoupon()
        {
            StringBuilder shortBuffer = new StringBuilder();
            string strguid = Guid.NewGuid().ToString("N");
            for (int i = 0; i < 8; i++)
            {
                String str = strguid.Substring(i * 4, 4);
                int x = Convert.ToInt32(str, 16);

                shortBuffer.Append(chars[x % chars.Length]);
            }
            return shortBuffer.ToString().ToLower();

        }


        /// <summary>
        /// 获取当前日期字符串
        /// </summary>
        public static string GetDateStr()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 获取用户分享码(刻意封装)
        /// </summary>
        public static int GetShareCodeByUserId(int userId)
        {
            return userId;
            //return (userId + 2234) * 3;
        }

        /// <summary>
        /// 解码获取用户ID(刻意封装)
        /// </summary>
        public static int GetUserIdByShareCode(int userId)
        {
            return userId;
            //try
            //{
            //    if (userId == 0)
            //    {
            //        return 0;
            //    }

            //    return (userId / 3) - 2234;
            //}
            //catch (Exception)
            //{

            //    return 0;
            //}
        }
     

        /// <summary>
        /// 根据日期获取星期几
        /// </summary>
        public static string GetWeekByDate(DateTime dt)
        {
            var day = new[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
            return day[Convert.ToInt32(dt.DayOfWeek.ToString("d"))];
        }

        /// <summary>
        /// 得到这个月的第几周
        /// </summary>
        /// <param name="daytime">年月日</param>
        /// <returns>传递过来的时间是第几周</returns>
        public static int GetWeekNumInMonth(DateTime daytime)
        {
            int dayInMonth = daytime.Day;
            //本月第一天
            DateTime firstDay = daytime.AddDays(1 - daytime.Day);
            //本月第一天是周几
            int weekday = (int)firstDay.DayOfWeek == 0 ? 7 : (int)firstDay.DayOfWeek;
            //本月第一周有几天
            int firstWeekEndDay = 7 - (weekday - 1);
            //当前日期和第一周之差
            int diffday = dayInMonth - firstWeekEndDay;
            diffday = diffday > 0 ? diffday : 1;
            //当前是第几周,如果整除7就减一天
            int weekNumInMonth = ((diffday % 7) == 0
                ? (diffday / 7 - 1)
                : (diffday / 7)) + 1 + (dayInMonth > firstWeekEndDay ? 1 : 0);
            return weekNumInMonth;
        }
        /// <summary>
        /// 判断一个字符串是否为url
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsUrl(string str)
        {
            try
            {
                string Url = @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$";
                return Regex.IsMatch(str, Url);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
        public static bool CheckUserName(string str)
        {
            try
            {
                string rg = @"^[a-z][a-z0-9-_]*$";
                return Regex.IsMatch(str, rg);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 计算密码强度
        /// </summary>
        /// <param name="password">密码字符串</param>
        /// <returns></returns>
        public static bool PasswordStrength(string password)
        {
            //空字符串强度值为0
            if (string.IsNullOrEmpty(password)) return false;

            //字符统计
            int iNum = 0, iLtt = 0, iSym = 0;
            foreach (char c in password)
            {
                if (c >= '0' && c <= '9') iNum++;
                else if (c >= 'a' && c <= 'z') iLtt++;
                else if (c >= 'A' && c <= 'Z') iLtt++;
                else iSym++;
            }

            if (iLtt == 0 && iSym == 0) return false; //纯数字密码
            if (iNum == 0 && iLtt == 0) return false; //纯符号密码
            if (iNum == 0 && iSym == 0) return false; //纯字母密码

            if (password.Length >= 6 && password.Length < 16) return true;//长度不大于6的密码

            if (iLtt == 0) return true; //数字和符号构成的密码
            if (iSym == 0) return true; //数字和字母构成的密码
            if (iNum == 0) return true; //字母和符号构成的密码

            return true; //由数字、字母、符号构成的密码
        }
    }
}
